<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Model extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		$this->isValidRequest();
		//加载model模型
		$this->load->model('backend/Model_model', 'Model');
		//加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示后台个人中心下的查看模块
	 */
	public function index() {
		$result = $this->Model->getModelAll($this->page, $this->input->post());
		$data = $this->getList();
		foreach ($result['data'] as &$v) {
			$v['parent'] = (empty($data[$v['parent']])?'主类': trim($data[$v['parent']], '&nbsp;')) . " ({$v['parent']})";
		}
		$result['modelList'] = $data;
		$this->load->view('backend/model/index', $result);
	}

	/**
	 * 添加模块
	 */
	public function add() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'name',
				'url',
				'parent',
				'rank',
				'target',
				'width',
				'height'
					), array(
				array('name', 'required'),
				array('url', 'required'),
				array('parent', 'integer'),
				array('rank', 'integer'),
				array('target', 'required|in_list[navTab,dialog]'),
				array('width', 'integer'),
				array('height', 'integer'),
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'name' => $this->form_validation->name,
				'url' => $this->form_validation->url,
				'rel' => str_replace('/', '', $this->form_validation->url),
				'parent' => $this->form_validation->parent,
				'rank' => $this->form_validation->rank,
				'target' => $this->form_validation->target,
				'width' => $this->form_validation->width,
				'height' => $this->form_validation->height,
				'created_at' => time()
			);
			if ($this->Model->addModel($data)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendmodelindex',
					'callbackType' => 'closeCurrent'));
			}
			$this->renderJson(300);
		}
		$data['modelList'] = $this->getList();
		$this->load->view('backend/model/add', $data);
	}

	/**
	 * 删除模块
	 */
	public function delete() {
		$ids = $this->input->post_get('ids');
		if ($this->Model->deleteModel($ids)) {
			$this->renderJson(200);
		}

		$this->renderJson(300);
	}

	/**
	 * 修改模块信息
	 */
	public function edit() {
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'id',
				'name',
				'url',
				'parent',
				'rank',
				'target',
				'width',
				'height'
					), array(
				array('id', 'integer|greater_than[0]'),
				array('name', 'required'),
				array('url', 'required'),
				array('parent', 'integer'),
				array('rank', 'integer'),
				array('target', 'required|in_list[navTab,dialog]'),
				array('width', 'integer'),
				array('height', 'integer'),
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'name' => $this->form_validation->name,
				'url' => $this->form_validation->url,
				'rel' => str_replace('/', '', $this->form_validation->url),
				'parent' => $this->form_validation->parent,
				'rank' => $this->form_validation->rank,
				'target' => $this->form_validation->target,
				'width' => $this->form_validation->width,
				'height' => $this->form_validation->height,
			);
			if ($this->Model->updateModel($data, $this->form_validation->id)) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendmodelindex',
					'callbackType' => 'closeCurrent'));
			}
			$this->renderJson(300);
		}
		$id = intval($this->get('id'));
		$data = $this->Model->findModel($id);
		$data['modelList'] = $this->getList();
		$this->load->view('backend/model/edit', $data);
	}

	/**
	 * 获取列表
	 */
	private function getList() {
		$config = array('select'=>'id,name,parent');
		$result = $this->Model->getModelAll($config);
		$data = array();
		if(!empty($result['data'])) {
			$this->getChild($data, $result['data'], 0);
		}
		return $data;
	}

	private function getChild(&$data, $result, $parent=0, $times=0) {
		foreach ($result as $v) {
			if($v['parent'] == $parent) {
				$data[$v['id']] = str_pad($v['name'], strlen($v['name']) + $times * 24, '&nbsp;', STR_PAD_LEFT);
				$this->getChild($data, $result, $v['id'], $times + 1);
			}
		}
	}
}
